// 商品管理脚本
$(function() {
	$("#searchid").linkbutton({
		 text:'查询',
		 iconCls:'icon-search'
	});
	$('#messagegrid').datagrid({
		title : '您所在的位置：首页信息模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;特价抢购首页推荐',
		idField : 'id',
		width : 1000,
		height : 600,
		nowrap : false,
		striped : true,
		collapsible : false,
		url : '../goods/goods_findAllGoodsByCategoryId.shtml',
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					field : 'name',
					title : '商品名称',
					align : 'center',
					width : 162
				},{
					field : 'imgSrc',
					title : '商品图片',
					align : 'center',
					width : 82,
					formatter:function(value,rec){
						return '<img width="80px" height="80px" src="../'+value+'">';
					}
				},{
					field : 'price',
					title : '商品原价',
					align : 'center',
					width : 82,
					formatter:function(value,rec){
						var unit = rec['unit'];
						if(unit == undefined)
							unit = '元';
						return value+''+unit;
					}
				},{
					field : 'upprice',
					title : '商品抢购价',
					align : 'center',
					width : 82,
					formatter:function(value,rec){
						var unit = rec['unit'];
						if(unit == undefined)
							unit = '元';
						return value+unit;
					}
				}, {
					field : 'activity',
					title : '是否有效',
					align : 'center',
					width : 145,
					sortable : true
				}, {
					field : 'weights',
					title : '权重',
					align : 'center',
					width : 145,
					sortable : true
				}, {
					field : 'id',
					title : '操作',
					align : 'center',
					width : 160,
					sortable : true,
					formatter : function(value, rec) {
						var id = rec.id;
						var weights = rec['weights'];
						if (rec.id == undefined)
							return null;
						else
							return '<span style="color:red" id="operate"><a href="javascript:modify(\''+weights+'\',\''+id+'\')">修改</a></span>';
					}
				}
		]],
		pagination : true,
		rownumbers : true,
		pageList:[10,20,50,80,100],
		toolbar:[
		{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}
		]
	});

	
	$.get("../brand/showFirstCategory.shtml",function(mes){
		if(mes!=''){
			var json = eval('('+mes+')');
			//var str = '<select name="categoryId" id="categoryId" >';
			var str = '';
			for(var i = 0; i < json.length; i++){
				str += '<input type="button" value="'+json[i]['name']+'" onclick="onSearch(\''+json[i]['id']+'\')"/>&nbsp;&nbsp;&nbsp;&nbsp;';
				//str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
//			str+='</select>';
			$('#categoryBigSpan').html(str);
			onSearch(json[0]['id']);
		}
		
	})
	$("#modifyWindow").dialog({
		title:'修改',
		width:300,
		height:120,
		resizable:false,
		minimizable:false,
		maximizable:false,
		toolbar: [{
			text : '保存信息',
		    iconCls:'icon-save',
		    handler:function(){
						var url1 = "../goods/goods_modifyWeights.shtml";
						var weights = $("#weightValue").val();
						var brandId = $("#brandIdValue").val();
						if(isNaN(weights)){
							alert("权重值只能为数字");
							return;
						}
						$.ajax({
							url : url1,
							data : "goodId=" + brandId+"&weights="+weights,
							success : function(msg) {
								if(msg=="1"){
									alert("保存成功");							
								}else{
									alert("保存失败");
								}
									$("#modifyWindow").dialog('close');
								$('#messagegrid').datagrid("reload");
								
							}
						});
		    }
		  }],
		closed:true
	})
});

function modify(weights,brandId) {
	$("#weightValue").val(weights);
	$("#brandIdValue").val(brandId);
	$("#modifyWindow").window('open');
}

function onSearch(categoryId){
	if(categoryId==""){
		alert("请选择类别");
		return;
	}
	var queryParams =$('#messagegrid').datagrid("options").queryParams;
	queryParams.categoryId=categoryId;
	$('#messagegrid').datagrid("reload");
}